package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.UtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WebSiteMapper;
import com.ruoyi.system.domain.WebSite;
import com.ruoyi.system.service.IWebSiteService;
import com.ruoyi.common.core.text.Convert;

/**
 * 网址Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-13
 */
@Service
public class WebSiteServiceImpl implements IWebSiteService 
{
    @Autowired
    private WebSiteMapper webSiteMapper;

    /**
     * 查询网址
     * 
     * @param id 网址ID
     * @return 网址
     */
    @Override
    public WebSite selectWebSiteById(String id)
    {
        return webSiteMapper.selectWebSiteById(id);
    }

    /**
     * 查询网址列表
     * 
     * @param webSite 网址
     * @return 网址
     */
    @Override
    public List<WebSite> selectWebSiteList(WebSite webSite)
    {
        return webSiteMapper.selectWebSiteListByPage(webSite);
    }

    /**
     * 新增网址
     * 
     * @param webSite 网址
     * @return 结果
     */
    @Override
    public int insertWebSite(WebSite webSite)
    {
        webSite.setCreateTime(DateUtils.getNowDate());
        return webSiteMapper.insertWebSite(webSite);
    }

    /**
     * 修改网址
     * 
     * @param webSite 网址
     * @return 结果
     */
    @Override
    public int updateWebSite(WebSite webSite)
    {
        webSite.setUpdateTime(DateUtils.getNowDate());
        return webSiteMapper.updateWebSite(webSite);
    }

    /**
     * 删除网址对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWebSiteByIds(String ids)
    {
        return webSiteMapper.deleteWebSiteByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除网址信息
     * 
     * @param id 网址ID
     * @return 结果
     */
    @Override
    public int deleteWebSiteById(String id)
    {
        return webSiteMapper.deleteWebSiteById(id);
    }

    /**
     * 查询默认网址列表
     * @param webSite
     * @return
     */
    @Override
    public List<WebSite> getSubscribeList(WebSite webSite) {
        return webSiteMapper.getSubscribeList(webSite);
    }

    /**
     * 获取唯一的一个网址
     * @param webSite
     * @return
     */
    @Override
    public WebSite getOneWebSite(WebSite webSite) {
        webSite.setWebUrl(UtilTool.subString(webSite.getWebUrl()));
        List<WebSite> sites = webSiteMapper.checkWebSite(webSite);
        List<WebSite> siteList = new ArrayList<>();
        WebSite data = null;
        for (WebSite site:sites){
            String url = UtilTool.subString(site.getWebUrl());
            if (webSite.getWebUrl().length()>=url.length()){
                siteList.add(site);
            }
        }
        if (siteList.size()!=0){
            return siteList.get(0);
        }else {
            return data;
        }
    }

    /**
     * 用户网址统计
     * @param webSite
     * @return
     */
    @Override
    public List<WebSite> getAppUserlist(WebSite webSite) {
        return webSiteMapper.getAppUserlist(webSite);
    }

    /**
     * 通过ids查询列表
     * @param siteIds
     * @return
     */
    @Override
    public List<WebSite> getWebSiteByIds(String siteIds) {
        return webSiteMapper.getWebSiteByIds(Convert.toStrArray(siteIds));
    }

    /**
     * app百宝箱搜索
     * @param webSite
     * @return
     */
    @Override
    public List<WebSite> getSearchList(WebSite webSite) {
        return webSiteMapper.getSearchList(webSite);
    }


}
